package com.common.utils;
public class SnowflakeIdGenerator {
    // 起始时间戳（可以自行定义）
    private final long twepoch = 1612790400000L;
    // 机器ID所占的位数
    private final long workerIdBits = 5L;
    // 数据中心ID所占的位数
    private final long datacenterIdBits = 5L;
    // 最大机器ID
    private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
    // 最大数据中心ID
    private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
    // 序列号所占的位数
    private final long sequenceBits = 12L;
    // 机器ID左移的位数
    private final long workerIdShift = sequenceBits;
    // 数据中心ID左移的位数
    private final long datacenterIdShift = sequenceBits + workerIdBits;
    // 时间戳左移的位数
    private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
    // 序列号掩码
    private final long sequenceMask = -1L ^ (-1L << sequenceBits);

    private long workerId;
    private long datacenterId;
    private long sequence = 0L;
    private long lastTimestamp = -1L;

//    public static void main(String[] args) {
//        SnowflakeIdGenerator snowflakeIdGenerator=new SnowflakeIdGenerator(1l,1l);
//        long l = snowflakeIdGenerator.nextId();
//        System.out.println();
//    }

    public SnowflakeIdGenerator(long workerId, long datacenterId) {
        if (workerId > maxWorkerId || workerId < 0) {
            throw new IllegalArgumentException(String.format("Worker ID can't be greater than %d or less than 0", maxWorkerId));
        }
        if (datacenterId > maxDatacenterId || datacenterId < 0) {
            throw new IllegalArgumentException(String.format("Data center ID can't be greater than %d or less than 0", maxDatacenterId));
        }
        this.workerId = workerId;
        this.datacenterId = datacenterId;
    }

    public  synchronized long nextId() {
        long timestamp = timeGen();
        if (timestamp < lastTimestamp) {
            throw new RuntimeException(
                    String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
        }
        if (timestamp == lastTimestamp) {
            sequence = (sequence + 1) & sequenceMask;
            if (sequence == 0) {
                timestamp = tilNextMillis(lastTimestamp);
            }
        } else {
            sequence = 0L;
        }
        lastTimestamp = timestamp;
        return ((timestamp - twepoch) << timestampLeftShift) |
                (datacenterId << datacenterIdShift) |
                (workerId << workerIdShift) |
                sequence;
    }

    private  long tilNextMillis(long lastTimestamp) {
        long timestamp = timeGen();
        while (timestamp <= lastTimestamp) {
            timestamp = timeGen();
        }
        return timestamp;
    }

    private  long timeGen() {
        return System.currentTimeMillis();
    }



//    public static void main(String[] args) {
//        SnowflakeIdGenerator snowflakeIdGenerator=new SnowflakeIdGenerator(1l,1l);
//        long l = snowflakeIdGenerator.nextId();
//        System.out.println(l);
//    }
}